<?php
class VmViewModel extends VmArrayModel
{
    protected $resourceId = 'SystemResource';
	protected static $instance;

	public static function GetInstance()
	{
        return self::$instance ? self::$instance : self::$instance = new VmViewModel();
	}

    public static function InsertDrilldownUrl()
    {
        $views = $this->Find();
        VmFieldModel::GetInstance()->InsertDrilldownUrl($views);
    }

    public function GetByModel($model_name, $fields = null)
    {
        $cond = new SqlCondition();
		$cond->AddString('model_id', $model_name);
		return $this->Find($cond, $fields);
    }

    public function GetNameByModel($model_name)
    {
        return $this->GetIdByFieldValue('model_id', $model_name);
    }

    public function ExportToSql($selecteds)
    {
    	$export = array();
        foreach ($selecteds as $id)
		{
			$export[] = $this->ExportToSqlById($id);
		}
		$out['content'] = "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";\n\n\n" . implode("\n\n\n", $export);
		$out['name'] = 'vm_view.sql';
		$out['type'] = 'text/x-sql';
		$out['file_export'] = true;
		return $out;
    }

    public function ExportToSqlById($id)
    {
    	$dump = null;
		$view = $this->FindById($id);
		if ($view)
		{
            $cond = $this->GetIdCondition($id);
			$view_insert = $this->GetSqlInsert($cond, 'model_id');

			$field_view_id = 'view_id';
            $cond = new SqlCondition();
            $cond->AddNumber($field_view_id, $view['id']);

			$set_last_insert = "SET @$field_view_id = LAST_INSERT_ID();";
			$actions_insert = VmActionModel::GetInstance()->GetSqlInsert($cond, $field_view_id);
            if ($actions_insert) $actions_insert .= "\n\n";
			$fields_insert = VmFieldModel::GetInstance()->GetSqlInsert($cond, $field_view_id);

			$dump = "-- VIEW {$view['id']}\n\n$view_insert\n\n"
                ."$set_last_insert\n\n"
                ."$actions_insert"
                ."$fields_insert\n";
		}
		return $dump;
    }

    public function Insert($item)
    {
        $item['model_id'] = String::Underscore($item['model_id']);
        if (!isset($item[self::FIELD_NEW_ID]))
            $item[self::FIELD_NEW_ID] = String::Pascalize($item['model_id']);
        $id = parent::Insert($item);
        VmFieldModel::GetInstance()->InsertFields($id);
        return $id;
    }

	protected function GetModelNameById($id)
	{
		return $this->FindScalarById($id, 'model_id');
	}

    public function GetModelById($id)
	{
		$model_name = VmViewModel::GetInstance()->GetModelNameById($id);
		return Model::GetInstanceByName($model_name);
	}

    public function Del($id)
    {
        $cond = new SqlCondition();
        $cond->AddString('view_id', $id);
        VmActionModel::GetInstance()->Delete($cond);
        VmFieldModel::GetInstance()->Delete($cond);
        return parent::Del($id);
    }

    public function IsTree($view_name)
    {
        $vm_view = $this->FindById($view_name);
        return $vm_view['viewtype_id'] == $this->config->VIEWTYPE_TREE;
    }

    protected function LoadFields()
    {
        $this->AddField('id', 'varchar', false, true);
        $this->AddField('model_id', 'varchar', false);
        $this->AddField('allow_edit', 'bool', false, false, 1);
        $this->AddField('allow_delete', 'bool', false, false, 1);
        $this->AddField('allow_insert', 'bool', false, false, 1);
        $this->AddField('viewtype_id', 'int', false, false, $this->config->VIEWTYPE_DEFAULT);
        $this->AddField('title', 'varchar', true);
        $this->AddField('description', 'varchar', true);

        $this->SetForeignKey('viewtype_id', VmViewtypeModel::GetInstance()->GetTableName(), 'id');
    }
}
?>